package com.apes.framework.plugin.form.parser.element;

import com.apes.framework.plugin.form.parser.node.FormNodeParser;
import com.apes.framework.plugin.filereader.xml.ApesElement;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 功能：page解析
 *
 * @author xul
 * @create 2018-04-16 16:12
 */
@Component
public class TabElementParser extends AbstractElementParser {
    private boolean hasGroup = false;
    private boolean hasTable = false;
    private boolean hasSplit = false;

    /**
     * 可解析的元素列表
     **/
    private final String[] elementNames = {"tab"};

    /**
     * 功能：可解析节点域
     */
    @Override
    public String getDomain() {
        return FormNodeParser.domain;
    }

    /**
     * 功能：可解析的元素列表
     */
    @Override
    public String[] getNames() {
        return elementNames;
    }


    @Override
    public Map parse(ApesElement element) {
        hasGroup = hasGroup(element);
        hasTable = hasTable(element);
        hasSplit = hasSplit(element);
        Map node = super.parse(element);
        element.getNestedElements().stream()
                .filter(e -> e.getName().equals("subtotal"))
                .map(new Function<ApesElement, Object>() {
                    @Override
                    public Object apply(ApesElement apesElement) {
                        node.put("subtotal", apesElement.parse());
                        return null;
                    }
                }).collect(Collectors.toList());
        return node;
    }

    /**
     * 功能：解析属性元素
     *
     * @param node
     * @param attributes
     */
    @Override
    protected void parseAttributes(Map node, Map<String, String> attributes) {
        node.put("id", "a");
        node.put("genre", hasGroup ? "group" : hasTable ? "grid" : hasSplit ? "split" : "form");
        attributes.forEach((k, v) -> {
            node.put(k.trim(), v.trim());
        });
    }

    /**
     * 功能：解析子节点
     *
     * @param node
     * @param elements
     */
    @Override
    protected void parseNestedElements(Map node, Collection<ApesElement> elements) {
        if (hasGroup) {
            Map m = new HashMap();
            m.put("renderData", elements.stream().filter(e -> e.getName().equals("group"))
                    .map(new Function<ApesElement, Object>() {
                        @Override
                        public Object apply(ApesElement apesElement) {
                            Map row = (Map) ((List) ((Map) apesElement.parse()).get("renderData")).get(0);
                            row.put("type", "div");
                            return row;
                        }
                    }).collect(Collectors.toList()));
            node.put("data", m);
        } else {
            node.put("fields", elements.stream()
                    .filter(e -> !e.getName().equals("subtotal"))
                    .map(new Function<ApesElement, Object>() {
                        @Override
                        public Object apply(ApesElement apesElement) {
                            return apesElement.parse();
                        }
                    }).collect(Collectors.toList()));
        }
    }

    private boolean hasGroup(ApesElement element) {
        return element.getNestedElements().stream().anyMatch(e -> e.getName().equals("group"));
    }

    private boolean hasTable(ApesElement element) {
        // 表格外层可能有其他嵌套（field,group等）,所以这里用到递归寻找
        return element.getNestedElements().stream().anyMatch(e -> e.getName().equals("table"))
                || element.getNestedElements().stream().anyMatch(
                (e) -> {
                    return hasTable(e);
                }
        );
    }

    private boolean hasSplit(ApesElement element) {
        return element.getNestedElements().stream().anyMatch(e -> e.getName().equals("split"));
    }
}
